// pages/music/music.js
Page({

  /**
   * 页面的初始数据
   */
  data : {
    current : 0,
    verticalCurrent : 2,
    src: "",
    name: "",
    tempName: "选择音乐文件",
    loop:false,
    loopimage: "/image/notloop.png",
    loopmode: "不循环",
    mode: "非切片模式",
    boolMode: false,
    a: "",
    b: "",
    disableToNextStep: true, // 选择文件之后才可以点击下一步
    result: []
  },

  handleClick (e) {  //下一步 上一步的处理
    const id = e.target.dataset.id;
    
    if(id === 'prev'){
      const addCurrent = this.data.current - 1;
      var current = addCurrent < 0 ? 0 :addCurrent;
    }
    else{
      const addCurrent = this.data.current + 1;
      var current = addCurrent > 2 ? 0 : addCurrent;
    }
    this.setData({
        'current' : current
    })
    console.log(this.data.mode, this.data.boolMode)
  },
  checkNumber(value){ // 检查是否为数字
      var re= /^[0-9]+.?[0-9]*$/;
      if(! re.test(value)) return false;
      return true;
  },
  setParam(id, value){
      if(id==='a'){
        this.setData({
          'a': value
        })
      }
      else if(id==='b'){
        this.setData({
          'b': value
        })
      }
  },
  inputParams(e){
      const id = e.target.dataset.id;
      const that = this;
      var flag = true;  // 输入合法性标志位，不合法就清空当前输入框
      var value = e.detail.detail.value.replace(/\s*/g,"");
      if(this.checkNumber(value)){
        const param = parseFloat(value);
        if(param>=0 && param<1){
            flag = false;
            that.setParam(id, value)
        }
        else{
          wx.showToast({
            title: "参数应0~1之间"
          })
        }
      }
      else{
        wx.showToast({
          title: "请输入正确的数字",
          icon: 'warn'
        })
      }
      if(flag){  // 如果输入有误 则清空输入框
        that.setParam(id, "");
      }
  },

  bindSubmit(e){
    const that = this;
    const a = this.data.a;
    const b = this.data.b;
    const mode = this.data.boolMode
    console.log(a, b, mode)
    wx.getStorage({
      key: 'musicbase64',
      success: res=>{
        const data = res.data;
        wx.showLoading({
          title: '识别中请稍后',
        })
        wx.request({
          url: 'http://127.0.0.1:8888/api/signal/genres',
          data: {
            data: data,
            a: a,
            b: b,
            slice: mode
          },
          method: 'POST',
          success: res => {
            const result = res.data.result;
            result.sort((a, b)=>b.prob-a.prob);
            that.setData({
              result: result
            })
            console.log(result);
          },
          complete: res=>{
            wx.hideLoading();
            that.setData({
              tempName: "选择音乐文件"
            })
            // wx.removeStorageSync('musicbase64');
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.removeStorageSync('musicbase64');
    this.audioCtx = wx.createAudioContext('myAudio')
  },
  audioPlay() {
    console.log("play1")
    this.audioCtx.play()
    console.log("play");
  },
  audioPause() {
    this.audioCtx.pause()
  },
  audioStart() {
    this.audioCtx.seek(0)
  },
  chooseMusic(e){
    const that = this;
    wx.showLoading({
      title: '正在读取，请稍后',
    });
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      extension:['wav','mp3','au'],
      success(res) {
       
        console.log(res)
        const tempFilePath = res.tempFiles[0]
        const path = tempFilePath.path
        const fs = wx.getFileSystemManager()
        fs.readFile({
          filePath: path,
          encoding: 'base64',
          success: res=>{
            console.log(res)
            // wx.hideLoading();
            that.setData({
              src: path,
              name: tempFilePath.name,
              tempName: tempFilePath.name,
              disableToNextStep: false
            })
            wx.setStorage({
              key: 'musicbase64',
              data: res.data,
            })
          },
          fail: res=>{
            console.log(res)
          },
          complete: res=>{
            console.log(res)
        
          }
        })
      },
      fail: res=>{
          console.log(res);
      },
      complete:res=>{
        wx.hideLoading()
      }
    })
  },
  switch1Change(e){
    var mode = "";
    if(e.detail.value) mode = "切片模式"
    else mode = "非切片模式"
    this.setData({
      mode: mode,
      boolMode: ! this.data.boolMode
    })
  },

  loop(e){
    const state = this.data.loop;
    var image = "/image/loop.png";
    var loopmode = "循环播放"
    if(state){
      image = "/image/notloop.png";
      loopmode = "不循环"
    }else{
      image = "/image/loop.png";
      loopmode = "循环播放"
    }
    this.setData({
      loop: !state,
      loopimage: image,
      loopmode: loopmode
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})